import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author LKQ
 * @date 2021/12/5 10:01
 * @description 使用迭代的方法，与递归等价，但是需要显示的将栈模拟出来
 */
public class Solution2 {
    public static void main(String[] args) {
        TreeNode leaf6 = new TreeNode(6, null, null);
        TreeNode leaf5 = new TreeNode(5, null, null);
        TreeNode leaf4 = new TreeNode(4, null, null);
        TreeNode leaf3 = new TreeNode(3, leaf6, null);
        TreeNode leaf2 = new TreeNode(2, leaf4, leaf5);
        TreeNode root = new TreeNode(1, leaf2, leaf3);
        Solution2 solution = new Solution2();
        solution.inorderTraversal(root);
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        Deque<TreeNode> stk = new LinkedList<TreeNode>();
        while (root != null || !stk.isEmpty()) {
            while (root != null) {
                stk.push(root);
                root = root.left;
            }
            root = stk.pop();
            res.add(root.val);
            root = root.right;
        }
        return res;
    }
}
